<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/26
 * Time: 21:16
 */

namespace Admin\Controller;


use Common\Model\TypeAttrModel;

class TypeAttrController extends CommenController
{
	public function index(){
		$tid = I('get.tid');
//		$this->p($tid);
		$typeAttr = (new TypeAttrModel())->where("tid=$tid")->select();
//		$this->p($typeAttr);
		$this->assign('typeAttr',$typeAttr);
		$this->display();
	}
	/**
	 * 添加属性及规格
	 */
	public function add(){
		$tid = I('get.tid');
//		$this->p($tid);
		if(IS_POST){
//			$this->p($_POST);
			$_POST['tavalue'] = str_replace('|',',',trim($_POST['tavalue']));
//			$this->p($_POST);
			$typeAttr = (new TypeAttrModel())->add($_POST);
			if ($typeAttr){
				$this->success('添加成功',U('index',['tid'=>$_POST['tid']]));
			}else{
				$this->error('添加失败');
			}

		}


		$this->display();
	}
	/**
	 * 修改属性及规格
	 */
	public function edit(){
		$taid = I('get.taid');
//		$this->p($taid);
//		获取旧数据
		$oldData = (new TypeAttrModel())->where("taid=$taid")->find();
//		$this->p($oldData);
		$this->assign('oldData',$oldData);

		if(IS_POST){
			$_POST['taid'] =$taid;
//			$this->p($_POST);
			$typeAttr = (new TypeAttrModel())->where("taid=$taid")->save($_POST);
			if ($typeAttr){
				$this->success('修改成功',U('index',['tid'=>$_POST['tid']]));
				exit;
			}else{
				$this->error('修改失败');
				exit;
			}

		}

		$this->display();
	}
	public function del(){
		$taid = I('get.taid');
//		$this->p($taid);

		$data = new TypeAttrModel();
		$tid = $data->where("taid=$taid")->getField('tid');
//		$this->p($tid);
		if ($data->where("taid=$taid")->delete()){
			$this->success('删除成功',U('index',['tid'=>$tid]));
			exit;
		}else{
			$this->error('删除失败');
			exit;
		}

	}


}